package com.ts.api.module.api.entity.bo;

import com.gitee.apanlh.util.base.CollUtils;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMaskField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import lombok.Data;

import java.util.Comparator;
import java.util.List;

/**
 *  API掩码撞库业务实体
 *
 *  @author Pan
 */
@Data
public class ApiPhoneMaskDataExchangeBO {

    /** 当前请求id */
    private String reqId;
    /** 手机号掩码模式返回字段-多个模式 */
    private List<PlanDistributePhoneMaskField> listPhoneMaskField;

    public ApiPhoneMaskDataExchangeBO() {
        super();
    }

    /**
     *  构建
     *
     *  @param  reqId                                请求id
     *  @param  listPlanDistributePhoneMaskResult    分发结果集合
     *  @return ApiDataExchangeBO
     */
    public static ApiPhoneMaskDataExchangeBO of(String reqId, List<PlanDistributeResult> listPlanDistributePhoneMaskResult) {
        List<PlanDistributePhoneMaskField> copyListPhoneMaskField = CollUtils.newArrayList();

        //  合并字段
        for (PlanDistributeResult planDistributeResult : listPlanDistributePhoneMaskResult) {
            PlanDistributeField planDistributeField = planDistributeResult.getPlanDistributeField();
            List<PlanDistributePhoneMaskField> listPhoneMaskField = planDistributeField.getListPhoneMaskField();
            copyListPhoneMaskField.addAll(listPhoneMaskField);
        }

        //  单价最高排序最优先
        copyListPhoneMaskField.sort(Comparator.comparing(PlanDistributePhoneMaskField::getPrice, Comparator.reverseOrder()));

        ApiPhoneMaskDataExchangeBO apiDataExchangeBO = new ApiPhoneMaskDataExchangeBO();
        apiDataExchangeBO.setReqId(reqId);
        apiDataExchangeBO.setListPhoneMaskField(copyListPhoneMaskField);
        return apiDataExchangeBO;
    }
}
